class MyPage extends BaseList {
    constructor(action, orderby, flag, $pagesize, $pagination, cokiePageSize, cokiePageIndex, cokieListResult, listUrl, delUrl) {
        super(action, orderby, flag, $pagesize, $pagination, cokiePageSize, cokiePageIndex, cokieListResult, listUrl, delUrl);
    }
    OnPageLoad() {
        // 回车直接进行搜索
        $("panel_search").bind('keyup', function (e) {
            var key = e.keyCode;
            if (key === 13) {
                _Instance.FetchForPage(1);
            }
        });
        $('#loading').hide();
    }
    SetCriterion(item) {
        item['StyleNo'] = $.trim($("#StyleNoFilterValue").val());
        item['MaterialNo'] = $.trim($("#MaterialNoFilterValue").val());
        return item;
    }
    GetFilter() {
        let item = super.GetFilter(0);
        // 设置条件
        item = this.SetCriterion(item);
        return JSON.stringify(item);
    }
    FetchForPage(pageIndex) {
        $.ajax({
            type: "POST",
            url: parent.basePath + this.listUrl,
            timeout: 30 * 1000,
            data: this.GetFilter(pageIndex),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: () => {
            }
        }).done(data => {
            if (data.HeaderEntity !== null) {
                //$("#SerialNo").text(data.HeaderEntity.serialNo);
                //$("#SoNo").text(data.HeaderEntity.soNo);
                $("#ProductName").text(data.HeaderEntity.productName);
                $("#StyleNo").text(data.HeaderEntity.styleNo);
                $("#Quantity").text(data.HeaderEntity.quantity);
            }
            if (data.LinesList !== null) {
                this.SetTableContent(data.LinesList);
            }
        }).fail(data => {
            ajaxError(data, 1);
        }).always(() => {
        });
    }
    SetTableContent(result) {
        $("#tab_data01 tbody").empty();

        let content_01 = '';
        this.rows = result.length;
        for (let i = 0; i < result.length; i++) {
            content_01 += '<tr >';
            content_01 += '<td rowspan="3">' + (i + 1) + '</td>';
            content_01 += '<td rowspan="3">' + formatNullData(result[i].materialName) + '</td>';
            content_01 += '<td rowspan="3">' + formatNullData(result[i].materialNo) + '</td>';
            content_01 += '<td rowspan="3">' + formatNullData(result[i].specification) + '</br>' + formatNullData(result[i].materialColourName) + '</td>';
            content_01 += '<td rowspan="3">' + formatNullData(result[i].poQuantity) + '</td>';
            content_01 += '<td rowspan="2" style="border-bottom:1px #000 dashed;"></td>';
            content_01 += '<td rowspan="2" style="border-bottom:1px #000 dashed;"></td>';
            content_01 += '<td rowspan="2" style="border-bottom:1px #000 dashed;"></td>';
            content_01 += '<td rowspan="2" style="border-bottom:1px #000 dashed;"></td>';
            content_01 += '<td rowspan="3"></td>';
            content_01 += '<td style="border-bottom:1px #000 dashed;">&nbsp;</td>';
            content_01 += '<td style="border-bottom:1px #000 dashed;"></td>';
            content_01 += '<td style="border-bottom:1px #000 dashed;"></td>';
            content_01 += '<td style="border-bottom:1px #000 dashed;"></td>';
            content_01 += '<td rowspan="3"></td>';
            content_01 += '<td rowspan="3"></td>';
            content_01 += '<td rowspan="3"></td>';
            content_01 += '</tr>';
            content_01 += '<tr>';
            content_01 += '<td style="border-top:1px #000 dashed; border-bottom:1px #000 dashed;">&nbsp;</td>';
            content_01 += '<td style="border-top:1px #000 dashed; border-bottom:1px #000 dashed;">&nbsp;</td>';
            content_01 += '<td style="border-top:1px #000 dashed; border-bottom:1px #000 dashed;">&nbsp;</td>';
            content_01 += '<td style="border-top:1px #000 dashed; border-bottom:1px #000 dashed;">&nbsp;</td>';
            content_01 += '</tr>';
            content_01 += '<tr>';
            content_01 += '<td style="border-top:1px #000 dashed;">&nbsp;</td>';
            content_01 += '<td style="border-top:1px #000 dashed;"></td>';
            content_01 += '<td style="border-top:1px #000 dashed;"></td>';
            content_01 += '<td style="border-top:1px #000 dashed;"></td>';
            content_01 += '<td style="border-top:1px #000 dashed;"></td>';
            content_01 += '<td style="border-top:1px #000 dashed;"></td>';
            content_01 += '<td style="border-top:1px #000 dashed;"></td>';
            content_01 += '<td style="border-top:1px #000 dashed;"></td>';
            content_01 += '</tr>';

            $("#tab_data01 tbody").append(content_01);
            content_01 = '';
        }
        setOneTableRowColor("tab_data01");

        this.AutoAdjustTableHeightForLines();   // 自适应高度设置
        $(window).resize();                     // 防止因为出现滚动条而造成画面显示问题
    }
    //《按钮》搜索
    DoSearch() {
        if (formatNullInputValue($.trim($("#StyleNoFilterValue").val())) === null) {
            ShowErrorMessagePanel('款号检索条件不能空。');
            return false;
        } else {
            this.FetchForPage(1);
        }
    }
    AutoAdjustTableHeightForLines() {
        let h = $(window).height();
        let _rc = Math.floor((h - 188 - 20) / 60);
        let _dh = _rc * 60;
        let _tah = this.rows * 60;
        _dh = (_tah >= _dh) ? _dh : _tah;

        $("#div_content").css({"max-height": _dh, height: _dh});
    }
    ExportToExcel() {
        $("#export_form").attr("action", parent.basePath + "/inventory/report/material_inventory_book/export_to_excel.html");
        $("#ExportStyleNo").val($("#StyleNoFilterValue").val());
        $("#ExportMaterialNo").val($("#MaterialNoFilterValue").val());
        $("#export_form").submit();
    }
}

var _Instance = null;

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    $("#search_btn").hide(); // 无需搜索，隐藏搜索按钮
    // 设置数据补齐功能
    $("#StyleNoFilterValue").bigAutocomplete({
        url: parent.basePath + "/master/product/fetchSerialNoForAutoComplate.html"
    });
    _Instance = new MyPage(0, null, null, null, null, null, null, null,
            "/inventory/report/material_inventory_book/fetch.html",
            "");

    // 用于导出excel文件
    $("#export_iframe").remove();
    let $iFrame = $('<iframe id="export_iframe" name="_export_iframe" style="position:absolute; top:-9999px; left:-9999px" src="javascript:false"></iframe>');
    $("body").append($iFrame);
    let $form = $('<form id="export_form" name ="_export_form" method="POST" action ="" target="_export_iframe" style="position:absolute; top:-1200px; left:-1200px"></form>');
    $('<input type="hidden" id="ExportStyleNo" name="_ExportStyleNo" value="" />').appendTo($form);
    $('<input type="hidden" id="ExportMaterialNo" name="_ExportMaterialNo" value="" />').appendTo($form);
    $("body").append($form);
    // 监听iframe回调（submit()后设置iFrame加载完成事件，用于获取表单提交成功后的结果,iFrame.load(loadFunc());）
    $iFrame.on("load", function () {
        let contentWin = document.getElementById("export_iframe").contentWindow;
        let backBodyText = contentWin.document.body.innerText;
    });

    _Instance.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
    if (_Instance !== null)
        _Instance.AutoAdjustTableHeightForLines();
}
